package com.ssy.lingxi.contract.serviceimpl;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.date.DateUtil;
import com.ssy.lingxi.common.response.PageData;
import com.ssy.lingxi.common.response.Wrapper;
import com.ssy.lingxi.contract.model.dto.OperateLogInnerResponDto;
import com.ssy.lingxi.contract.model.dto.OperateLogInputDto;
import com.ssy.lingxi.contract.model.dto.OperateLogOuterResponDto;
import com.ssy.lingxi.contract.model.vo.common.request.OperateLogRequest;
import com.ssy.lingxi.contract.model.vo.common.response.OperateLogInnerResponse;
import com.ssy.lingxi.contract.model.vo.common.response.OperateLogOuterResponse;
import com.ssy.lingxi.contract.service.IContractInnerRecordService;
import com.ssy.lingxi.contract.service.IContractOuterRecordService;
import com.ssy.lingxi.contract.service.IOperateLogService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

/**
 *  平台后台-系统管理-操作日志
 * @author ds
 * @version 2.0.0
 * @date 2021/06/17
 */
@Slf4j
@Service
public class OperateLogServiceImpl implements IOperateLogService {

    @Resource
    private IContractInnerRecordService contractInnerRecordService;
    @Resource
    private IContractOuterRecordService contractOuterRecordService;

    @Override
    public Wrapper<PageData<OperateLogOuterResponse>> pageOuterLog(OperateLogRequest request) {
        OperateLogInputDto dto=new OperateLogInputDto();
        BeanUtils.copyProperties(request,dto);
        PageData<OperateLogOuterResponDto> data = contractOuterRecordService.pageListCommon(null, dto);
        List<OperateLogOuterResponDto> dataList = data.getData();
        List<OperateLogOuterResponse> responseList=null;
        if(CollectionUtil.isNotEmpty(dataList)){
            responseList = dataList.stream().map(o -> {
                OperateLogOuterResponse response = new OperateLogOuterResponse();
                BeanUtils.copyProperties(o, response);
                response.setOperateTime(DateUtil.format(DateUtil.date(o.getOperateTime()), "yyyy-MM-dd HH:mm"));
                response.setStatus(o.getStatusName());
                return response;
            }).collect(Collectors.toList());
        }
        return Wrapper.success(new PageData<>(data.getTotalCount(),responseList));
    }



    @Override
    public Wrapper<PageData<OperateLogInnerResponse>> pageInnerLog(OperateLogRequest request) {
        OperateLogInputDto dto=new OperateLogInputDto();
        BeanUtils.copyProperties(request,dto);
        PageData<OperateLogInnerResponDto> data = contractInnerRecordService.pageListCommon(null, dto);
        List<OperateLogInnerResponDto> dataList = data.getData();
        List<OperateLogInnerResponse> responseList=null;
        if(CollectionUtil.isNotEmpty(dataList)){
            responseList = dataList.stream().map(o -> {
                OperateLogInnerResponse response = new OperateLogInnerResponse();
                BeanUtils.copyProperties(o, response);
                response.setOperateTime(DateUtil.format(DateUtil.date(o.getOperateTime()), "yyyy-MM-dd HH:mm"));
                response.setStatus(o.getStatusName());
                return response;
            }).collect(Collectors.toList());
        }
        return Wrapper.success(new PageData<>(data.getTotalCount(),responseList));
    }
}
